﻿/* ============================================================================== 
 * 功能描述：TaskPanel 
 * 创 建 者：Administrator
 * 创建日期：2016/5/16 21:48:40 
 * ==============================================================================*/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZZQFrameWork;

public class TaskPanel:BaseUI
{
    /// <summary>
    /// 任务标题
    /// </summary>
    private Text taskTitleText;

    /// <summary>
    /// 任务描述
    /// </summary>
    private Text taskDesText;

    /// <summary>
    /// 接受任务按钮，任务完成时变成完成任务按钮
    /// </summary>
    private GameObject acceptButton;

    /// <summary>
    /// 接受任务按钮下的text，任务完成时变成完成任务
    /// </summary>
    private Text acceptButtonText;

    /// <summary>
    /// 任务奖励物品gird
    /// </summary>
    private GridLayoutGroup rewardGoodsGridLayoutGroup;

    /// <summary>
    /// 任务奖励数值grid
    /// </summary>
    private GridLayoutGroup rewardNumberGridLayoutGroup;

    private GameObject rewardExpItem;
    private GameObject rewardGoldItem;

    /// <summary>
    /// 当前显示任务数据
    /// </summary>
    private TaskData data;

    public override Enum_UIType GetUIType()
    {
        return Enum_UIType.TaskPanel;
    }

    void Awake()
    {
        taskTitleText = transform.GetComponentByPath<Text>("TaskTitle/Text");
        taskDesText = transform.GetComponentByPath<Text>("TaskDes/Text");
        acceptButton = transform.Find("AcceptButton").gameObject;
        acceptButtonText = transform.GetComponentByPath<Text>("AcceptButton/Text");
        rewardGoodsGridLayoutGroup = transform.GetComponentByPath<GridLayoutGroup>("RewardGoodsGrid");
        rewardNumberGridLayoutGroup = transform.GetComponentByPath<GridLayoutGroup>("RewardNumberGrid");
        rewardExpItem = rewardNumberGridLayoutGroup.transform.Find("RewardExpItem").gameObject;
        rewardGoldItem = rewardNumberGridLayoutGroup.transform.Find("RewardGoldItem").gameObject;

        EventTriggerListener.Get(acceptButton).SetEventHandle(Enum_TouchEventType.OnClick, OnAcceptTask);
    }

    void OnEnable()
    {
        EventDispatcher.AddListener<TaskData>(Events.TaskEvent.ShowTask, OnShowTask);
    }
    void OnDisable()
    {
        EventDispatcher.RemoveListener<TaskData>(Events.TaskEvent.ShowTask, OnShowTask);
    }

    /// <summary>
    /// 显示任务
    /// </summary>
    /// <param name="data"></param>
    private void OnShowTask(TaskData data)
    {
        this.data = data;
        taskTitleText.text = data.name;
        taskDesText.text = data.des;
        ShowReward();

        if (data.taskProgress == Enum_TaskProgress.Finished)
        {
            acceptButtonText.text = "完成任务";
        }
        else
        {
            acceptButtonText.text = "接受任务";
        }
    }

    /// <summary>
    /// 显示奖励
    /// </summary>
    private void ShowReward()
    {
        for (int i = 0; i < data.rewardDataList.Count; i++)
        {
            TaskRewardData reward = data.rewardDataList[i];
            if (reward.rewardType == Enum_TaskRewardType.Goods || reward.rewardType == Enum_TaskRewardType.Equip)
            {
                GameObject go = PoolManager.Instance.Spawn("RewardGoodsItem");
                go.transform.SetParent(rewardGoodsGridLayoutGroup.transform, false);
                MainPanelRewardGoodsItem item = go.GetComponent<MainPanelRewardGoodsItem>();
                item.SetData(reward.rewardType, reward.rewardId, reward.count);
            }
            else if (reward.rewardType == Enum_TaskRewardType.Exp)
            {
                rewardExpItem.gameObject.SetActive(true);
                rewardExpItem.GetComponentInChildren<Text>().text = reward.count.ToString();
            }
            else if (reward.rewardType == Enum_TaskRewardType.Gold)
            {
                rewardGoldItem.gameObject.SetActive(true);
                rewardGoldItem.GetComponentInChildren<Text>().text = reward.count.ToString();
            }
        }
    }

    private void OnAcceptTask(GameObject _listener, object _args, object[] _params)
    {
        Debug.Log("点击按钮");
        if (data.taskProgress == Enum_TaskProgress.Finished)
        {
            Debug.Log("任务完成，点击完成");
            ModuleManager.Instance.Get<CharacterModule>().mainRole.FinishTaskToNpc(data.id);
        }
        else
        {
            Debug.Log("任务没有，接收任务");
            ModuleManager.Instance.Get<CharacterModule>().mainRole.AcceptTask(data);
            UIManager.Instance.CloseUI(Enum_UIType.TaskPanel);
        }
    }

    protected override void OnClose()
    {
        Debug.Log("关闭任务界面");
        foreach (Transform t in transform)
        {
            PoolManager.Instance.UnSpawn(t.gameObject);
        }
        rewardExpItem.gameObject.SetActive(false);
        rewardGoldItem.gameObject.SetActive(false);
    }
}
